<?php
class CodeEvent extends Event {
  function __construct($timeout=180) {
    $this->TTL = (Time() + $timeout);
  }

  /**
   * Make a New Trigger
   *
   * @param mixed $RegExp regulerni vyraz, muze byt zadan jako jeden string, nebo jako pole stringu
   * @param string $Code kod ktery se pusti pokud bude regulerni vyraz odpovidat
   * @param boolean $OnTimeout nastavi priznak ze se ma tato spoust aktivovat pokud uplyne zivotnost udalosti
   */
  public function AddTrigger($RegExp, $Code, $OnTimeout = false)
  {
    if (!is_array($RegExp)) $RegExp = array($RegExp);
    foreach ($RegExp as $Exp )
    {
      static $i = 0;
      while (isset ($this->Trigger[$i])) $i++;
      $this->Trigger[$i] = array (
      'Part' => $Code,
      'RegExp' => $Exp,
      'OnTimeout' => $OnTimeout
      );
    }
  }
  
  
  /**
   * Otestuje vsechny triggery v udalosti a pripadne pusti kod
   *
   * @param string $IRCLine line of IRC comunication witch will be checked
   * @return mixed pri uspechu true, jinak cislo chyby
   */
  public function ResumeEvent($IRCLine) {
    if (!$this->IsValid()) {
      foreach ($this->Trigger as &$Exp)
  	   {
  	     if ($Exp['OnTimeout'])
  	     {
  	       $this->SafeEval($Exp['Part']);
  	       return CEvent_Expired;
  	     }
  	   }
  	   return CEvent_Expired;
    }
    else
  	 {
  	   //print_r($this->Trigger);
  	   foreach ($this->Trigger as &$Exp)
  	   {
  	     //echo 'testuji z codeeventu regexp ['.$Exp['RegExp'].'] na ['.$IRCLine.']';
  	     if (($Exp['RegExp'] != false) && ereg($Exp['RegExp'], $IRCLine))
  	     {
  	       eval($Exp['Part']);
  	       return false;
  	     }
  	   }
  	 }
  	 return false;
  }

}
?>